$(function () {

    // 全局文本框状态变换
    $('.input_t,.input').bind({
        'focus': function () {
            $(this).addClass('onFoucs');
        },
        'blur': function () {
            $(this).removeClass('onFoucs');
        }
    });

    // 点击切换验证码
    $(".reloadVerify").click(function () {
        var img = $(".verifyImg").attr("src");
        if (img.indexOf('?') > 0) {
            $(".verifyImg").attr("src", (img + '&random=') + Math.random());
        } else {
            $(".verifyImg").attr("src", (img.replace(/\?.*$/, '') + '?') + Math.random());
        }
    });

    /** 投资人切换到借款人身份 **/
    $("#switchIdentity").click(function () {
        switch_user();
        return false;
    });

    auto_s();
    $(window).resize(function () {
        auto_s();
    })
});


var input_key;
if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)) {
    input_key = "keyup";
} else {
    input_key = "input propertychange";
}

var switch_user = function () {
    layer.confirm('您当前是投资人身份，无法进行借款，需要退出登陆后选择借款人重新登陆，确认要退出登录吗？',{
        btn: ['确定', '取消'], //按钮
        title: "温馨提醒"
    },function(){
        window.location.href = "/uc/sign/logout";
    })
    // layer.confirm('您当前是投资人身份，无法进行借款，确认要切换成借款人吗？', {
    //     btn: ['确定', '取消'], //按钮
    //     title: "温馨提醒"
    // }, function () {
    //     $.post("/uc/index/switch_user.html", {}, function (data) {
    //         if (data.code === 1) {
    //             var refer = data.data || data.url;
    //             layer.msg(data.msg, {
    //                 time: 2000
    //             }, function () {
    //                 window.location.href = refer;
    //             });
    //
    //         } else {
    //             var refer = data.url ? data.url : '/';
    //             layer.alert(data.msg, {icon: 2},function (index) {
    //                 layer.close(index);
    //                 window.location.href = refer;
    //             });
    //         }
    //         return false;
    //     }, "json");
    //     return false;
    // });
};

// 展示红包
var cardShow = function () {
    $('#envelopes').fadeIn("fast");
};

// 关闭展示红包
var cardHide = function () {
    $("#envelopes").fadeOut("fast");
};

var getCookie = function (name) {
    return $.cookie(name);
};

function clickAgree(id) {
    // 协议
    $("#agree").click(function () {
        $(this).val($(this).prop("checked") === true ? 1 : 0);
    });
}

// 加载
var wjzxLoad = function (type,time) {
    var types = type ? type : 0;
    var timer = (time ? time : 10) * 1000;
    layer.load(types , {
        time: timer ,
        shade: [0.4, '#000']
    });
};

/** 银行存管提示关闭 **/
var closeBankPopup = function (e) {
    var url = $(e).find(".closeBtn").attr("data-href");
    var condition = $(e).find(".closeBtn").attr("data-status");
    condition === 'true' ? $(e).fadeOut("fast") : (url ? window.location.href = url : window.location.reload());
};

var openBankPopup = function (e, t) {
    $(e).fadeIn("fast").find(".closeBtn").attr("data-status", t === true ? true : '');
};

/** 登录弹窗 **/
var to_login = function (t,d) {
    var uid_id = "#UID";
    var uid = isNaN($(uid_id).val()) ? 0 : parseInt($(uid_id).val());
    if (uid <= 0) {
        var id = ".toLoginFixed";
        $(id).fadeIn("fast");
        $(id).find(".insetBox").animate({top: '50%'}, 500);
        $(".login_tab li").eq( d === true ? 1 : 0).click();
    } else {
        window.location.href = $(t).attr("data-href");
    }
};
var close_login = function () {
    var id = ".toLoginFixed";
    $(id).find(".insetBox").animate({top: '-100%'}, 500);
    $(".toLoginFixed").fadeOut("fast");
};

/** end **/

/*红包侧边栏*/
function auto_s() {
    var width = $(window).width() ,div = $(".redAmi");
    var num = width > 1430 ? (width - 1200) / 2 - (div.width() + 20) : 10;
    div.css("right", num);
}


// ajax提交
function ajax_post(obj, type, parme, action, pop) { //id - 类型 - 数据 - 地址 - 弹窗
    function postType() {
        if (parme && action) {
            $.post(action, parme, success, "json");
        } else {
            $.post(obj.attr("action"), obj.serialize(), success, "json");
        }
    }

    if (type === 0) {
        setTimeout(function () {
            postType()
        }, 1500);
    } else {
        postType()
    }
    return false;

    function success(data) {
        // type = > 注册:3  登录:2  充值:1
        if (data.code === 1) {

            if (type === 2) {
                var bankTips = $("#bankCloseBox"), condition = $("input[name='remember']").val(),
                    user = $("#mobile").val(), select_value = $("#isBorrow").val(), user_type = 0;
                $("#loginBtn").val("登录成功");
                $.cookie('login_error', null, {path: '/'});
                $.cookie("name", parseInt(condition) === 1 ? user : '', {path: "/", expires: 3});
                if (select_value == 1) {
                    user_type = 1;
                }
                $.cookie("userType", user_type, {path: "/", expires: 3});

                var refer = data.url || data.data.redirect_url;
                var bank_status = (data.data.deposit_status === 0 && data.data.audit_status !== 1 && is_bank === 1);
                /*
                deposit_status => 银行存管账户 1 -> 开通
                audit_status => 银行存管账户开户审核中 1 -> 审核中
                is_bank => 是否存管系统 1 -> 审核中
                */

                if (pop === true) {
                    if (bank_status) {
                        close_login();
                        setTimeout(function () {
                            bankTips.fadeIn("fast");
                        }, 500);
                    } else {
                        window.location.reload();
                    }
                    return false;
                } else {
                    if (bank_status) {
                        bankTips.find(".closeBtn").attr("data-href", data.data.redirect_url);
                        bankTips.fadeIn("fast");
                        return false;
                    } else {
                        setTimeout(function () {
                            window.location.href = refer.length > 0 ? refer : "/";
                        }, 1500);
                        return false;
                    }
                }
            } else {
                if (data.url || data.data) {
                    var refer = data.url || data.data;
                    layer.alert(data.msg, {icon: 1}, function () {
                        window.location.href = refer;
                    });
                    setTimeout(function () {
                        window.location.href = refer;
                    }, 1500);
                } else {
                    layer.alert(data.msg, {icon: 1});
                    setTimeout("location.reload()", 1500);
                }
            }
        } else {
            if (type === 3) {
                if (data.msg !== null) {
                    layer.alert(data.msg, {icon: 2});
                    return false;
                } else {
                    checkErrorType(data);
                }
                return false;
            }
            if (type === 2) {
                loginType(data, (pop == 2 ? true : false));
                return false;
            }
            if (type === 1) {
                layer.alert(data.msg, {icon: 2}, function (index) {
                    layer.close(index);
                    window.location.reload();
                });
                return false;
            }
            layer.alert(data.msg, {icon: 2});
            $("input[type='submit']").attr("disabled", false);
            return false;
        }
    }
}